<?php

/*
* 后台首页
*/

namespace app\admin\controller;
use app\admin\model\AuthRule;

use think\Controller;
use expand\Sitemap;

class Index extends Common
{
    public function index()
    {
        $userTotal = $this->userTotal();                    //会员总数
        $this->assign('userTotal', $userTotal);
        
        $archiveTotal = $this->archiveTotal();              //有效/无效
        $this->assign('archiveTotal', $archiveTotal);
        
        $userNear = $this->userNear();                      //今日注册用户数
        $this->assign('userNear', $userNear);
        
        $guestbookTotal = $this->guestbookTotal();          //黑名单
        $this->assign('guestbookTotal', $guestbookTotal);
        
        $WaitThingList = $this->WaitThingList();              //待办事项
        $this->assign('WaitThingList', $WaitThingList);
                
        $DaycountLineJson = $this->DaycountLineJson();      //天统计
        $this->assign('DaycountLineJson', $DaycountLineJson);

        $WeekcountLineJson = $this->WeekcountLineJson();      //周统计
        $this->assign('WeekcountLineJson', $WeekcountLineJson);
		
        $MonthcountLineJson = $this->MonthcountLineJson();      //月统计
        $this->assign('MonthcountLineJson', $MonthcountLineJson);
		
        // $systemConfig = $this->systemConfig();              //服务器信息配置
        // $this->assign('systemConfig', $systemConfig);
		
        return $this->fetch();
    }
    
    /**
     * @Title: memberTotal
     * @Description: todo(用户总数)
     * @throws
     */
    private function userTotal()
    {
        $MemberModel = new \app\admin\model\Member;
        return $userTotal = $MemberModel->count();
    }
    
    /**
     * @Title: userNear
     * @Description: todo(今日注册用户)
     * @throws
     */
    private function userNear()
    {
        $MemberModel = new \app\admin\model\Member;
        $userTime = strtotime(date('Y-m-d 00:00',time()));
        return $userNear = $MemberModel->where('create_time', 'egt', $userTime)->count();
    }
    
    /**
     * @Title: archiveTotal
     * @Description: todo(有效/无效)
     * @throws
     */
    private function archiveTotal()
    {
        $MemberModel = new \app\admin\model\Member;
        return $archiveTotal = $MemberModel->field('sum(case when is_check=1 then 1 else 0 end) as youxiao,sum(case when is_check=0 then 1 else 0 end) as wuxiao')->find();
    }
    
    /**
     * @Title: guestbookTotal
     * @Description: todo(黑名单)
     * @throws
     */
    private function guestbookTotal()
    {
        $guestbookModel = new \app\admin\model\member;
        return $guestbookTotal = $guestbookModel->where('is_disable', 'eq', 0)->count();
    }
    
    /**
     * @Title: WaitThingList
     * @Description: todo(待办事项)
     * @throws
     */
    private function WaitThingList()
    {
        $cashModel = new \app\admin\model\Getcash;
        return $WaitThingList = $cashModel
								->field('a.*,m.username')
								->alias('a')
								->join("tf_member m","a.user_id=m.id","LEFT")
								->where('a.status','lt','2')->limit(10)->order('a.date DESC')->select();
    }
    
    /**
     * @Title: DaycountLineJson
     * @Description: todo(近30天统计)
     * @throws
     */
    private function DaycountLineJson()
    {
        $rechargeModel = new \app\admin\model\Recharge;
        $cashModel = new \app\admin\model\Getcash;

        $DaycountLineJson = [];
        $DaycountLineJson['datasets'][0]['name'] = lang('recharge_count');
        $DaycountLineJson['name'] = [lang('recharge_count'),lang('getcash_count')];
        $DaycountLineJson['datasets'][0]['type'] = 'line';
        $DaycountLineJson['datasets'][1]['type'] = 'line';
        $DaycountLineJson['datasets'][0]['smooth'] = true;
        $DaycountLineJson['datasets'][1]['smooth'] = true;
        $DaycountLineJson['datasets'][1]['name'] = lang('getcash_count');
        $DaycountLineJson['labels'] = $this->getDay30();
		$rechargeTotal = $CashTotal = [];
		foreach($DaycountLineJson['labels'] as $v){
			$money = $rechargeModel->where("status=2 and date_format(date,'%Y-%m-%d')='$v'")->sum('money');
			$rechargeTotal[] = empty($money) ? 0 : $money;
			$cashmoney = $cashModel->where("status=2 and date_format(date,'%Y-%m-%d')='$v'")->sum('money');
			$CashTotal[] = empty($cashmoney) ? 0 : $cashmoney;
		}
		$DaycountLineJson['datasets'][0]['data'] = $rechargeTotal;
		$DaycountLineJson['datasets'][1]['data'] = $CashTotal;
        return $DaycountLineJson = json_encode($DaycountLineJson);
    }
	/*
	 * @function WeekcountLineJson
	 * @Description (周统计)
	 */
	private function WeekcountLineJson(){
		
        $rechargeModel = new \app\admin\model\Recharge;
        $cashModel = new \app\admin\model\Getcash;
        $WeekcountLineJson = [];
        $WeekcountLineJson['datasets'][0]['name'] = lang('recharge_count');
        $WeekcountLineJson['name'] = [lang('recharge_count'),lang('getcash_count')];
        $WeekcountLineJson['datasets'][0]['type'] = 'line';
        $WeekcountLineJson['datasets'][1]['type'] = 'line';
        $WeekcountLineJson['datasets'][0]['smooth'] = true;
        $WeekcountLineJson['datasets'][1]['smooth'] = true;
        $WeekcountLineJson['datasets'][1]['name'] = lang('getcash_count');
		$WeekcountLineJson['labels'] = ['第一周','第二周','第三周','第四周'];
        $labels = $this->getWeek4();
		$rechargeTotal = $CashTotal = [];
		foreach($labels as $v){
			$where['status'] = array('eq',2); 
			$where['date'] = array('between',$v['start'].','.$v['end']); 
			$money = $rechargeModel->where($where)->sum('money');
			$rechargeTotal[] = empty($money) ? 0 : $money;
			$cashmoney = $cashModel->where($where)->sum('money');
			$CashTotal[] = empty($cashmoney) ? 0 : $cashmoney;
		}
		$WeekcountLineJson['datasets'][0]['data'] = $rechargeTotal;
		$WeekcountLineJson['datasets'][1]['data'] = $CashTotal;
        return $WeekcountLineJson = json_encode($WeekcountLineJson);
	}	
	/*
	 * @function WeekcountLineJson
	 * @Description (月统计)
	 */
	private function MonthcountLineJson(){
		
        $rechargeModel = new \app\admin\model\Recharge;
        $cashModel = new \app\admin\model\Getcash;
        $MonthcountLineJson = [];
        $MonthcountLineJson['datasets'][0]['name'] = lang('recharge_count');
        $MonthcountLineJson['name'] = [lang('recharge_count'),lang('getcash_count')];
        $MonthcountLineJson['datasets'][0]['type'] = 'line';
        $MonthcountLineJson['datasets'][1]['type'] = 'line';
        $MonthcountLineJson['datasets'][0]['smooth'] = true;
        $MonthcountLineJson['datasets'][1]['smooth'] = true;
        $MonthcountLineJson['datasets'][1]['name'] = lang('getcash_count');
        $MonthcountLineJson['labels'] = $this->getMonth6();
		$rechargeTotal = $CashTotal = [];
		foreach($MonthcountLineJson['labels'] as $v){
			$money = $rechargeModel->where("status=2 and date_format(date,'%Y-%m')='$v'")->sum('money');
			$rechargeTotal[] = empty($money) ? 0 : $money;
			$cashmoney = $cashModel->where("status=2 and date_format(date,'%Y-%m')='$v'")->sum('money');
			$CashTotal[] = empty($cashmoney) ? 0 : $cashmoney;
		}
		$MonthcountLineJson['datasets'][0]['data'] = $rechargeTotal;
		$MonthcountLineJson['datasets'][1]['data'] = $CashTotal;
        return $MonthcountLineJson = json_encode($MonthcountLineJson);
	}
    /**
     * @Title: systemConfig
     * @Description: todo(服务器信息配置)
     * @throws
     */
    private function systemConfig()
    {
        return $config = [
            '操作系统' => PHP_OS,
            '服务器时间' => date("Y-n-j H:i:s"),
            '运行环境' => $_SERVER["SERVER_SOFTWARE"],
            'PHP运行方式' => php_sapi_name(),
            '上传附件限制' => ini_get('upload_max_filesize'),
            '执行时间限制' => ini_get('max_execution_time').'秒',
        ];
    }
    /*
	*   @function getDay30
	*	@Description: 获取最近30天的时间
	*/
	private function getDay30(){
		$time = time();
		$result = array();
		for($i = $time-(86400*30);$i<$time;$i+=86400){
		  $result[] = strftime('%Y-%m-%d',$i);
		}
		return $result;
	}
    /*
	*	@function: getWeek7
	*	@Description: 获取最近4周的时间
	*/
	private function getWeek4(){
		$result = []; 
		$beginWeek = mktime(0,0,0,date("m"),date("d")-date("w")+1,date("Y"));
		$endWeek = mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"));
        $timestep = 604800; 
		for($i=3;$i>=0;$i--){
			$week['start'] = date('Y-m-d H:i:s',$beginWeek-$timestep*$i);
			$week['end'] = date('Y-m-d H:i:s',$endWeek-$timestep*$i);
			$result[]=$week;
		}
		return $result;
	}
    /*
	*	@function: getMonth6
	*	@Description: 获取最近6个月的时间
	*/
	private function getMonth6(){
		$result = array();
		for ($i=5;$i>=0;$i--){
			$result[] = date('Y-m',strtotime('-'.$i.' month'));
		}
		return $result;
	}
    /**
     * @Title: cleanCache
     * @Description: todo(清除缓存)
     * @throws
     */
    public function cleanCache()
    {
        if (request()->isPost()){
            deldir(RUNTIME_PATH);
            return ajaxReturn(lang('action_success'));
        }else{
            deldir(RUNTIME_PATH);
            return $this->fetch();
        }
    }
    
	/*sitemap 网站地图*/
	public function siteMap(){
		$site = new Sitemap();
		$aModel = new AuthRule();
		$where = [
			  'ismenu' => 1,
			  'status' => 1,
			  'module' => 'member',
			  'level'  => 1,
		];
		$cate = $aModel->where($where)->order('sorts asc')->select();
		$site->AddItem(input('server.REQUEST_SCHEME') . '://' . input('server.SERVER_NAME'), 1);
		foreach ($cate as $v)
		{
			$site->AddItem(input('server.REQUEST_SCHEME') . '://' . input('server.SERVER_NAME').'/'.$v['name'], 1);
		}
		$site->SaveToFile('sitemap.xml');
		return $this->fetch();
	}
	
    public function icons()
    {
        return $this->fetch();
    }
    
    public function forms()
    {
        return $this->fetch();
    }
    
    public function box()
    {
        return $this->fetch();
    }
    
    public function tab()
    {
        return $this->fetch();
    }
    
    public function tables()
    {
        return $this->fetch();
    }
    
    public function question()
    {
        return $this->fetch();
    }
}
